package com.javer.web.action;

import java.net.URLDecoder;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.springframework.stereotype.Controller;

import com.javer.domain.Comment;
import com.javer.domain.Company;
import com.javer.domain.KeyVal;
import com.javer.domain.PageView;
import com.javer.service.CompanyService;
import com.javer.util.Constants;
import com.javer.util.IpAddressUtil;
import com.javer.util.WebUtil;

@Controller("/company")
public class CompanyAction extends DispatchAction {
	private final Log logger = LogFactory.getLog(getClass());
	@Resource private CompanyService companyService;
	
	// 管理平台搜索
	public ActionForward searchCompanyByName(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse resp) throws Exception {
		PageView<Company> pv = WebUtil.createPageView(req);
		String name = req.getParameter("name");
		companyService.findCompany(pv, name);
		req.setAttribute("pv", pv);
		req.setAttribute("companys", pv.getRecords());
		req.setAttribute("name", name);
		return new ActionForward("/manage/company/list.jsp");
	}
	
	public ActionForward deleteCompany(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse resp) throws Exception {
		int id = Integer.valueOf(req.getParameter("id"));
		companyService.deleteCompany(id);
		return new ActionForward("/manage/company/list.jsp");
	}
	
	/**
	 * 管理平台新增公司
	 */
	public ActionForward saveCompany(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse resp) throws Exception {
		Company company = new Company();
		BeanUtils.populate(company, req.getParameterMap());
		
		try {
			companyService.addCompany(company);
			return new ActionForward("/company.do?method=getCompanyByPage", true);
		} 
		catch (Exception e) {
			logger.error(e.getMessage(), e);
			req.setAttribute("error", e.getMessage());
		}
		return new ActionForward("/manage/company/form.jsp");
	}
	
	/**
	 * 管理平台修改公司
	 */
	public ActionForward updateCompany(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse resp) throws Exception {
		Company company = new Company();
		BeanUtils.populate(company, req.getParameterMap());
		
		try {
			companyService.updateCompany(company);
			return new ActionForward("/company.do?method=getCompanyByPage", true);
		} 
		catch (Exception e) {
			logger.error(e.getMessage(), e);
			req.setAttribute("error", e.getMessage());
		}
		return new ActionForward("/manage/company/form.jsp");
	}
	
	// 跳转到修改页面
	public ActionForward updateUI(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse resp) throws Exception {
		int id = Integer.valueOf(req.getParameter("id"));
		Company company = companyService.getCompanyById(id);
		
		// 查询公司评论信息
		req.setAttribute("comments", companyService.getCommentsByCompany(id));
		req.setAttribute("company", company);
		return new ActionForward("/manage/company/form.jsp");
	}
	
	public ActionForward getCompanyByPage(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse resp) throws Exception {
		PageView<Company> pv = WebUtil.createPageView(req);
		companyService.getCompanyByPage(pv);
		
		req.setAttribute("companys", pv.getRecords());
		req.setAttribute("pv", pv);
		return new ActionForward("/manage/company/list.jsp");
	}

	public ActionForward getAllNewCompany(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse resp) throws Exception {
		PageView<Company> pv = WebUtil.createPageView(req);
		req.setAttribute("pv", companyService.getNewCompany(pv));
		return new ActionForward("/pages/company/companypagelist.jsp");
	}
	
	public ActionForward getAllHotCompany(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse resp) throws Exception {
		PageView<Company> pv = WebUtil.createPageView(req);
		req.setAttribute("pv", companyService.getHotCompany(pv));
		return new ActionForward("/pages/company/companypagelist.jsp");
	}
	
	public ActionForward getAllGoodCompany(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse resp) throws Exception {
		PageView<Company> pv = WebUtil.createPageView(req);
		req.setAttribute("pv", companyService.getGoodCompany(pv));
		return new ActionForward("/pages/company/companypagelist.jsp");
	}
	
	public ActionForward getAllBadCompany(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse resp) throws Exception {
		PageView<Company> pv = WebUtil.createPageView(req);
		req.setAttribute("pv", companyService.getBadCompany(pv));
		return new ActionForward("/pages/company/companypagelist.jsp");
	}
	
	// 前台主页搜索框调用方法
	public ActionForward findCompany(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse resp) throws Exception {
		PageView<Company> pv = WebUtil.createPageView(req);
		String name = req.getParameter("name");
		
		if (name != null) {
			//name = new String(name.getBytes("ISO8859-1"), "UTF8");
			name = URLDecoder.decode(name, "UTF-8");
		}
		
		KeyVal kv = new KeyVal();
		kv.setKey("company");
		kv.setValue(name);
		
		req.setAttribute("keywords", companyService.getHotCompanyNameKeyword(name, 8));
		companyService.addCompanyNameKeyword(kv);
		companyService.findCompany(pv, name);
		
		req.setAttribute("name", name);
		req.setAttribute("pv", pv);
		//req.setAttribute("companys", pv.getRecords());
		return new ActionForward("/pages/company/findcompanypagelist.jsp");
	}
	
	public ActionForward getHotCompanyKeywords(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse resp) throws Exception {
		int limit = 10;
		if (req.getParameter("limit") != null) {
			limit = Integer.valueOf(req.getParameter("limit"));
		}
		req.setAttribute("keywords", companyService.getHotCompanyNameKeyword("", limit));
		return mapping.findForward("list-hotkeyword");
	}
	
	public ActionForward getNewComment(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse resp) throws Exception {
		req.setAttribute("comments", companyService.getNewComment(15));
		return mapping.findForward("list-comment");
	}

	public ActionForward getNewCompany(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse resp) throws Exception {
		int limit = 5;
		if (req.getParameter("limit") != null) {
			limit = Integer.valueOf(req.getParameter("limit"));
		}
		req.setAttribute("companys", companyService.getNewCompany(limit));
		return mapping.findForward("list-company");
	}
	
	public ActionForward getGoodCompany(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse resp) throws Exception {
		req.setAttribute("companys", companyService.getGoodCompany(5));
		return mapping.findForward("list-company");
	}
	
	public ActionForward getBadCompany(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse resp) throws Exception {
		req.setAttribute("companys", companyService.getBadCompany(5));
		return mapping.findForward("list-company");
	}
	
	public ActionForward getHotCompany(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse resp) throws Exception {
		req.setAttribute("companys", companyService.getHotCompany(5));
		return mapping.findForward("list-company");
	}
	
	public ActionForward addCompanyComment(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse resp) throws Exception {
		Comment comment = new Comment();
		BeanUtils.populate(comment, req.getParameterMap());
		WebUtil.handleCommentContent(comment);
		comment.setUaddress(IpAddressUtil.getIpAddressByIp(comment.getUip()));
		int score = Integer.valueOf(req.getParameter("score"));
		
		String authCode = String.valueOf(req.getParameter("authCode"));
		String authCodeFromSession = (String) req.getSession().getAttribute(Constants.AuthCode);
		req.getSession().removeAttribute(Constants.AuthCode);
		
		if (!authCode.equalsIgnoreCase(authCodeFromSession)) {
			req.setAttribute("error", "验证码错误！");
			return new ActionForward("/company.do?method=showCompany&id=" + comment.getTid());
		}
		
		Company company = new Company();
		company.setId(comment.getTid());
		company.setCscore(score);
		
		companyService.addCompanyComment(company, comment);
		
		return new ActionForward("/company.do?method=showCompany&id=" + comment.getTid(), true);
	}
	
	public ActionForward updateCompanyScore(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse resp) throws Exception {
		Company company = new Company();;
		BeanUtils.populate(company, req.getParameterMap());
		companyService.updateCompanyScore(company);
		return null;
	}
	
	public ActionForward showCompany(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse resp) throws Exception {
		int id = Integer.valueOf(req.getParameter("id"));
		Company company = companyService.getCompanyById(id);
		
		// 查询公司评论信息
		req.setAttribute("comments", companyService.getHotCommentsByCompany(id));
		req.setAttribute("company", company);
		return mapping.findForward("show-company");
	}
	
	// 网友发布一个新的公司
	public ActionForward addCompany(ActionMapping mapping, ActionForm form,
			HttpServletRequest req, HttpServletResponse resp) throws Exception {
		String authCode = String.valueOf(req.getParameter("authCode"));
		String authCodeFromSession = (String) req.getSession().getAttribute(Constants.AuthCode);
		req.getSession().removeAttribute(Constants.AuthCode);
		
		if (!authCode.equalsIgnoreCase(authCodeFromSession)) {
			req.setAttribute("error", "验证码错误！");
			return mapping.findForward("add-company");
		}
		
		Company company = new Company();
		BeanUtils.populate(company, req.getParameterMap());
		
		try {
			int id = companyService.addCompany(company);
			return new ActionForward("/company.do?method=showCompany&id=" + id, true);
		} 
		catch (Exception e) {
			logger.error("保存公司时异常：" + e.getMessage(), e);
			req.setAttribute("error", "保存数据时异常：" + e.getMessage());
		}
		return mapping.findForward("add-company");
	}
	
	
}
